<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function __autoload($class_name)
{
    require_once "../class/" . $class_name . ".class.php";
}

try {
    $db = new DBHelper();
    if (isset($_GET['id']) && $_GET['id'] > 0) {
        $id = $_GET['id'];
        $sql = "SELECT * FROM t_vova WHERE id=?";
        $data = $db->execSQL($sql, array('i',$id), false);
    } else {
        $sql = "SELECT * FROM t_vova ORDER BY RAND() LIMIT 0,1";
        $data = $db->execSQL($sql, array(), false);
    }
    extract($data[0]);

    $facebook = new FacebookApi();
    if ($facebook->_getMyUid() == 0) {
        $facebook->_error();
        exit;
    }

    $arrChaSearch = explode(';', $character);

    $total = count($arrChaSearch)-1;
    $arrChaNew = array();
    $arrCha = array();
    $arrChaNew[0]['name'] = $_SESSION['name'];
    $arrChaNew[0]['uid'] = $_SESSION['uid'];
    $arrCha[] = $_SESSION['name'];

    if ($total > 0) {
        // $fql = "SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
        // and sex = 'female' and online_presence IN ('active', 'idle') order by rand() limit " . $total;
        $fql = "SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) order by rand() limit " . $total;
        $ret_obj = $facebook->api(array(
                'method' => 'fql.query',
                'query' => $fql,
        ));

        $i= 1;
        foreach ($ret_obj as $key => $value) {
            $arrChaNew[$i]['name'] = $value['name'];
            $arrChaNew[$i]['uid'] = $value['uid'];
            $arrCha[] = $value['name'];
            if (count($arrChaSearch)-1 == $i) {
                break;
            }
            $i++;
        }
    }

    $_SESSION['friends'] = $arrChaNew;

    $cm = new Common();
    $title = $cm->replaceCharacter($title, $arrChaSearch, $arrCha);
    $content = $cm->replaceCharacter($content, $arrChaSearch, $arrCha);
    $_SESSION['title'] = $title;
    $_SESSION['content'] = $content;

} catch(FacebookApiException $e) {
    echo $e->getMessage() . '<br/>';
    $facebook->_error();
    exit;
}
?>